#include <bits/stdc++.h>
using namespace std;
const int SIZE = 2e5 + 7;
int T;
int n;
int a[SIZE], b[SIZE], cnt[SIZE * 10], nxt[SIZE];
long long ans;
bool v[SIZE], vis[SIZE];
vector <int> chosen, unchosen;
void calc(int x){
    if (x == n + 1){
        chosen.clear();
        unchosen.clear();
        long long sum = 0;
        for (int i = 1; i <= n; i ++){
            if (v[i]) chosen.push_back(a[i]);
            else unchosen.push_back(a[i]);
        }
        for (int i = 1; i < chosen.size(); i ++)
            if (chosen[i] == chosen[i - 1]) sum += chosen[i], chosen[i] = -1;
        for (int i = 1; i < unchosen.size(); i ++)
            if (unchosen[i] == unchosen[i - 1]) sum += unchosen[i], unchosen[i] = -1;
        ans = max(ans, sum);
        return;
    }
    v[x] = 1;
    calc(x + 1);
    v[x] = 0;
    calc(x + 1);
}
void solve(){
    memset(v, 0, sizeof v);
    memset(vis, 0, sizeof vis);
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i ++) b[i] = a[i];
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; i ++) cnt[b[i]] ++;
    bool flag = true;
    for (int i = 2; i <= n; i ++)
        if (b[i] == b[i - 1]) {flag = false; break;}
    if (flag) {puts("0"); return;}
    if (n <= 15){
        calc(1);
        printf("%lld\n", ans);
        return;
    }
    for (int i = 1; i <= n; i ++) cnt[b[i]] = floor((double)(cnt[b[i]] / 2));
    for (int i = 1; i <= n; i ++){
        if (!cnt[a[i]]) continue;
        for (int j = i + 1; j <= n; j ++)
            if (!vis[j] && a[i] == a[j]) nxt[i] = j, vis[j] = 1;
    }
    
    for (int i = 1; i <= n; i ++){
        memset(vis, 0, sizeof vis);
        long long sum = 0;
        if (!cnt[a[i]]) continue;
        for (int j = i; j <= n; j ++){
            if (!cnt[a[j]]) continue;
            vis[j] = 1;
            sum += a[j];
            j = nxt[j];
        }
        chosen.clear();
        for (int j = 1; j <= n; j ++)
            if (!vis[j]) chosen.push_back(a[j]);
        for (int j = 1; j < chosen.size(); j ++)
            if (chosen[j] == chosen[j - 1]) sum += chosen[j], chosen[j] = -1;
        ans = max(ans, sum);
    }
    printf("%lld\n", ans);

}
int main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    scanf("%d", &T);
    while (T --) solve();
    return 0;
}
/*
3
3
1 2 1
4
1 2 3 4
8
3 5 2 5 1 2 1 4

1
0
8
*/